package com.example.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.httpMessageConvert.Property;
import com.example.demo.httpMessageConvert.weixin.AccessToken;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

@Component
public class WeixinUtil {
    @Autowired
    private Property property;
    private static HttpClient hc = HttpClients.createDefault();

    /**
     * 文件上传
     *
     * @param filePath
     * @param accessToken
     * @param type
     * @return
     * @throws IOException
     */
    public  String upload(String filePath, String accessToken, String type) throws IOException {
        File file = new File(filePath);
        //存在且是文件
        if (file.exists() && file.isFile()) {
            HttpPost httppost = new HttpPost(property.getUploadUrl()+"?access_token="+accessToken+"&type="+type);
            String BOUNDARY = "----------" + System.currentTimeMillis();
            httppost.addHeader("Content-Type", "multipart/form-data;boundary="+ BOUNDARY);
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
            multipartEntityBuilder.setBoundary(BOUNDARY);
            multipartEntityBuilder.addBinaryBody("media", file,
                    ContentType.APPLICATION_OCTET_STREAM, file
                            .getName());
            httppost.setEntity(multipartEntityBuilder.build());
            HttpResponse httpResponse = hc.execute(httppost);
           if( httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
               HttpEntity httpEntity = httpResponse.getEntity();
               JSONObject jsonObj = JSON.parseObject(EntityUtils.toString(httpEntity, "utf-8"));
               System.out.printf("");
           }

        }
        return null;

    }




    /**
     * 获取accessToken
     *
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public AccessToken getAccessToken() throws IOException {
        HttpGet httpget = new HttpGet(property.getToken()+"?grant_type=client_credential&appid="+property.getAppid()+"&secret="+property.getSecret());
        HttpResponse httpResponse = hc.execute(httpget);
        AccessToken token = new AccessToken();
        if( httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            HttpEntity httpEntity = httpResponse.getEntity();
            JSONObject jsonObj = JSON.parseObject(EntityUtils.toString(httpEntity, "utf-8"));
            token.setAccess_token(jsonObj.getString("access_token"));
            token.setExpires_in(jsonObj.getIntValue("expires_in"));
        }
        return token;
    }
    public JSONObject authorize(String url) throws IOException {
        HttpGet httpget = new HttpGet(url);
        HttpResponse httpResponse = hc.execute(httpget);
        if( httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            HttpEntity httpEntity = httpResponse.getEntity();
            JSONObject jsonObj = JSON.parseObject(EntityUtils.toString(httpEntity, "utf-8"));
           return  jsonObj;
        }

        return null;
    }
}
